﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace XHFrame
{
    /*角色控制器,技能等处理方式类似*/

    /// <summary>
    /// 基础角色属性控制框架
    /// </summary>
    public class BaseActor : IDynamicProperty
    {
        protected Dictionary<int, PropertyItem> diPropertyItem = null;

        public event PropertyChangeHand PropertyChange;

        public EnumActorType ActorType { get; set; }

        public int ID { get; set; }

        Debuger log = Debuger.GetLogger(typeof(BaseActor));

        private BaseScene corrntScene;

        public BaseScene CorrntScene
        {
            get
            {
                return corrntScene;
            }
            // 这里会处理场景切换的一些逻辑 暂时不切换;
            set
            {
                // 添加改变创景
                corrntScene = value;
            }
        }

        /// <summary>
        /// 清空属性类型
        /// </summary>
        public void ClearProperty()
        {
            if (diPropertyItem != null)
                diPropertyItem.Clear();
        }

        /// <summary>
        /// 删除属性类型
        /// </summary>
        /// <param name="propertyType">需要删除属性的类型</param>
        public void RemoveProperty(EnumPropertyType propertyType)
        {
            RemoveProperty((int)propertyType);
        }

        /// <summary>
        /// 删除属性类型
        /// </summary>
        /// <param name="id">需要删除属性的ID</param>
        public void RemoveProperty(int id)
        {
            if (diPropertyItem != null && diPropertyItem.ContainsKey(id))
            {
                diPropertyItem.Remove(id);
            }
        }

        /// <summary>
        /// 添加属性类型
        /// </summary>
        /// <param name="enumPropertyType"></param>
        /// <param name="content"></param>
        public virtual void AddProperty(EnumPropertyType enumPropertyType, object content)
        {
            AddProperty((int)enumPropertyType, content);
        }

        /// <summary>
        /// 添加属性
        /// </summary>
        /// <param name="ID">需要添加的ID</param>
        /// <param name="Content">需要添加的内容</param>
        public virtual void AddProperty(int ID, object Content)
        {
            PropertyItem propertyItem = new PropertyItem(ID, Content);
            AddProperty(propertyItem);
        }
        /// <summary>
        /// 添加属性
        /// </summary>
        /// <param name="propertyItem">需要添加的对象类</param>
        public virtual void AddProperty(PropertyItem propertyItem)
        {
            if (diPropertyItem == null)
            {
                diPropertyItem = new Dictionary<int, PropertyItem>();

            }
            if (diPropertyItem.ContainsKey(propertyItem.ID))
            {
                // remove same property
            }
            diPropertyItem.Add(propertyItem.ID, propertyItem);
            propertyItem.Owner = this;
        }
        /// <summary>
        /// 返回属性类型
        /// </summary>
        /// <param name="enumPropertyType">需要分会的属性类型</param>
        /// <returns></returns>
        public virtual PropertyItem GetProperty(EnumPropertyType enumPropertyType)
        {
            return GetProperty((int)enumPropertyType);
        }

        /// <summary>
        /// 角色属性改变事件,重载该方法 可以在角色属性改变后对其它场景等物体做逻辑操作.
        /// </summary>
        /// <param name="id">属性ID</param>
        /// <param name="oldValue">旧的值</param>
        /// <param name="newValue">新的值</param>
        protected virtual void OnProptyChanged(int id, object oldValue, object newValue)
        {
            // 添加更新
            // 使用时 重载 (Module)
            // if (id == (int)EnumPropertyType.HP)
            // { .... }
        }

        #region IDynamicProperty接口的实现
        void IDynamicProperty.DoChangeProperty(int id, object oldValue, object newValue)
        {
            OnProptyChanged(id, oldValue, newValue);
            if (null != PropertyChange)
                PropertyChange(this, id, oldValue, newValue);
        }

        /// <summary>
        /// 返回属性类型
        /// </summary>
        /// <param name="enumPropertyType">需要分会的ID</param>
        /// <returns></returns>
        public PropertyItem GetProperty(int id)
        {
            if (null == diPropertyItem)
                return null;
            if (diPropertyItem.ContainsKey(id))
                return diPropertyItem[id];
            log.W("Actor diProperty has non property ID:" + id + "key");
            Debug.LogWarning("Actor diProperty has non property ID:" + id + "key");
            return null;
        }
        #endregion


    }


}
